<template>
	<view class='page_cont'>
		<view class='top_wrap'>
			<view class='title_wrap'>
				<view class='title'>{{type==3?'用户注册':'绑定手机号'}}</view>
				<view class='subtitle'>未注册用户验证后自动登录</view>
			</view>
			<image class='logo' src="https://ossdev0.jiabala.com/5y6xEH8MGybeH"></image>
		</view>
		<view class='box'>
		    <input class='input' maxlength="11" @input="input_mobile" placeholder="请输入手机号" placeholder-style="color:rgba(66,71,82,0.5);"></input>
		</view>
		<view class='box'>
		    <input class='input' type="number" maxlength="6" @input="input_code" placeholder="请输入验证码" placeholder-style="color:rgba(66,71,82,0.5);"></input>
			<view class="btn" @click="send_code()" v-if="show_btn">发送验证码</view>
			<view class='text' v-else>重新发送({{time}})</view>
		</view>
		<view class='send_btn' :style="{background:mobile&&code?'rgba(91,144,255,1)':'rgba(91,144,255,0.5)'}" @click="very_code()">{{type==3?'注册':'绑定'}}</view>
		<view class='agr_wrap' @click="change_agree()">
		    <image class='icon' :src="agree?'/static/icon46.png':'/static/icon45.png'"></image>
		    <view class='text'>同意家巴啦<text class='txt' @click.stop="nav_go(0)">《用户协议》</text>和<text class='txt' @click.stop="nav_go(1)">《隐私政策》</text></view>
		</view>
	</view>
</template>

<script>
	import config from "../../api/config.js"
	import util from "../../utils/util.js"
	import api from "../../api/api.js"
	import resume from "../../api/myResume.js"
	export default{
		data(){
			return{
				type: '', // 1-已绑微信 2-未绑微信 3-未绑appleId
				appleId: '',
				mobile: '',
				code: '',
				agree: true,
				show_btn: true,
				time: 59,
				bindtype: 1,
				openId: '',
				unionId:'',
				userinfo: '',
				user_address: ''
			}
		},
		onLoad(options) {
			// console.log(options)
			const info = uni.getStorageSync('unionId')
			this.openId=info.openId
			this.unionId=info.unionId
			this.userinfo=uni.getStorageSync("user_info")
			this.user_address=uni.getStorageSync("user_address");
			this.type=options.type
			if(options.appleId){
				this.appleId=options.appleId
			}
		},
		methods:{
			// 同意协议
			change_agree(){
				this.agree=!this.agree
			},
			// 提示
			show_toast(){
			    util.page_show_toast('请先勾选同意家巴啦《用户协议》和《隐私政策》',2000)
			},
			//手机号
			input_mobile(e){
				this.mobile=e.detail.value;
			},
			// 验证码
			input_code(e){
				this.code=e.detail.value;
			},
			// 发送验证码
			send_code(){
				if(!util.testMobile(this.mobile)||this.mobile==''){
					util.page_show_toast('手机号码输入有误',1500)
				}else{
					this.user_send_code()
				}
			},
			// 发送验证码
			user_send_code(){
				var params={
					"phone": this.mobile
				}
				config.send_code(params).then(res=>{
					// console.log(res)
					util.page_show_toast("验证码发送成功",1500);
					this.show_btn=false;
					var timer=setInterval(()=>{
						this.time--;
						if(this.time<=0){
							this.show_btn=true;
							this.time=59;
							clearInterval(timer)
						}
					},1000)
				})
			},
			// 验证手机验证码
			very_code(){
				if(!this.agree){
					this.show_toast()
					return false;
				}
				var params={
					"phone": this.mobile,
					"code": this.code
				}
				config.very_code(params).then(res=>{
					if(this.type==1){
						this.wx_bind_phone()
					}else if(this.type==2){
						this.wx_bind()
					}else if(this.type==3){
						this.login_by_apple()
					}
				})
			},
			// 绑定手机
			wx_bind_phone(){
			    var params={
			      "phone": this.mobile,
			      "unionId": this.unionId,
			    }
			    config.wx_bind_phone(params).then(res=>{
			        if(res.data.data){
						this.wx_login();
			        }else{
						util.page_show_toast('绑定出错，请稍后再试',2000)
			        }
			    })
			},
			// 绑定手机号和微信
			wx_bind(){
			    var params={
					"nickName": this.userinfo.nickName?this.userinfo.nickName:this.mobile,
					"phone": this.mobile,
					"avatar": this.userinfo.avatarUrl?this.userinfo.avatarUrl:'https://ossdev0.jiabala.com/aeQCeISaVcGXw',
					"sex": this.userinfo.gender,
					"unionId": this.unionId,
					"openId": this.openId,
					"bindType": this.bindtype,
					"thirdAppId": api.data.appId,
					"province": this.user_address?this.user_address.province:'',
					"city": this.user_address?this.user_address.city:'',
					"area": this.user_address?this.user_address.district:'',
					"town": this.user_address?this.user_address.town:'',
					"address": this.user_address?this.user_address.address:''
			    }
			    config.wx_bind(params).then(res=>{
					// 登录
			        this.wx_login();
			    })
			},
			// 微信登录
			wx_login(){
				var params={
					"unionId": this.unionId,
					"openId": this.openId,
					"bindType": this.bindtype,
					"thirdAppId": api.data.appId
				}
				config.wx_login(params).then(res=>{
					// console.log(res);
					var user=res.data.data;
					util.page_show_toast('登录成功',1500);
					uni.setStorageSync('userinfo', user.userInfo)
					uni.setStorageSync('token', user.token)
					uni.removeStorageSync("user_info")
					this.isFirStLogin()
				})
			},
			// 绑定appleId
			login_by_apple(){
				var params={
					"unionId": this.appleId,
					"phone": this.mobile
				}
				config.login_by_apple(params).then(res=>{
					// console.log(res)
					var user=res.data.data;
					util.page_show_toast('登录成功',1500);
					uni.setStorageSync('userinfo', user.userInfo)
					uni.setStorageSync('token', user.token)
					this.isFirStLogin()
				})
			},
			isFirStLogin(){
				config.isFirstLogin().then(res=>{
					// console.log(res)
					// if(res.data.data.accountBusinesses==null&&res.data.data.accountProfile==null){
					// 	uni.navigateTo({
					// 		url:'../bindIdentity/bindIdentity'
					// 	})
					// }else {
					// 	uni.switchTab({
					// 		url:'../index/index'
					// 	})
					// }
					if (res.data.data.accountProfile == null && res.data.data.accountBusinesses == null) {
						// 
						this.get_org_list()
					} else if(res.data.data.accountProfile!==null && res.data.data.accountProfile.isLogin===1){
						uni.setStorageSync("identityNum", 1)
						setTimeout(function() {
							uni.navigateBack({
								delta: 1,
								fail(){
									uni.switchTab({
										url: '/pages/index/index'
									})
								}
							})
						}, 1500)
						this.saveLoginStatus(res.data.data.accountProfile.id,2)
					}else if(res.data.data.accountBusinesses!==null && res.data.data.accountBusinesses.isLogin===1){
						uni.setStorageSync("identityNum", 2)
						uni.setStorageSync("userIdentity",res.data.data.accountBusinesses)
						setTimeout(function() {
							uni.navigateBack({
								delta: 1,
								fail(){
									uni.switchTab({
										url: '/pages/index/index'
									})
								}
							})
						}, 1500)
						this.saveLoginStatus(res.data.data.accountBusinesses.id,1)
					}
				})
			},
			// 是否有认证机构
			get_org_list(){
				var params={
					"phone": uni.getStorageSync("userinfo").phone
				}
				config.get_org_list(params).then(res=>{
					var list=res.data.data;
					if(list&&list.length>0){
						var len=list.length;
						if(list[len-1].status==1){
							// 审核
							uni.redirectTo({
								url: '/pages/mechVerify/mechVerify?type=3&status=2'
							})
						}else if(list[len-1].status==2){
							// 审核通过
							uni.redirectTo({
								url: '/pages/mechVerify/mechVerify?type=2&status=2'+'&id='+list[len-1].businessId
							})
						}else if(list[len-1].status==4){
							// 审核未通过
							uni.redirectTo({
								url: '/pages/mechVerify/mechVerify?type=1&status=2'+'&id='+list[len-1].businessId
							})
						}
					}else{
						uni.navigateTo({
							url: '../bindIdentity/bindIdentity'
						})
					}
				})
			},
			// 记录下次状态
			saveLoginStatus(id,identity){
				resume.save_login_identity({id:id,identity:identity}).then(res=>{
					// console.log(res);
				})
			}
		}
	}
</script>

<style lang="scss">
	.page_cont{
		padding: 64rpx 48rpx;
		.top_wrap{
			width: 100%;
			display: flex;
			flex-direction: row;
			margin-bottom: 172rpx;
			.title_wrap{
				flex: 1;
				.title{
					text-align: left;
					height: 74rpx;
					font-size: 52rpx;
					font-weight: 400;
					line-height: 74rpx;
					color: #3F4A58;
					margin-bottom: 4rpx;
				}
				.subtitle{
					text-align: left;
					height: 42rpx;
					font-size: 30rpx;
					font-weight: 400;
					line-height: 42rpx;
					color: #929BA7;
				}
			}
			.logo{
				width: 120rpx;
				height: 120rpx;
				border-radius: 50%;
			}
		}
		.box{
			padding: 32rpx 32rpx 32rpx 44rpx;
			background: #F7F9FB;
			border-radius: 16rpx;
			display: flex;
			flex-direction: row;
			margin-bottom: 32rpx;
			.input{
				flex: 1;
				text-align: left;
				min-height: 36rpx;
				height: 42rpx;
				font-size: 30rpx;
				font-weight:400;
				line-height: 42rpx;
				color:rgba(66,71,82,1);
			}
			.btn{
				height: 42rpx;
				font-size: 30rpx;
				font-weight:400;
				line-height: 42rpx;
				color: #5B90FF;
			}
			.text{
				height: 42rpx;
				font-size: 30rpx;
				font-weight:400;
				line-height: 42rpx;
				color: rgba(91,144,255,0.5);
			}
		}
		.send_btn{
			margin: 48rpx 0 32rpx 0;
			height: 104rpx;
			line-height: 104rpx;
			text-align: center;
			font-size: 36rpx;
			border-radius: 16rpx;
			font-weight:600;
			color: #fff;
			letter-spacing:4rpx;
		}
		.agr_wrap{
			margin: 0 90rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			.icon{
				width: 28rpx;
				height: 28rpx;
				margin-right: 12rpx;
				margin-top: 2rpx;
			}
			.text{
				flex: 1;
				height: 34rpx;
				font-size: 24rpx;
				font-weight:400;
				line-height: 34rpx;
				color:rgba(146,155,167,0.5);
				.txt{
					color: #929BA7;
				}
			}
		}
	}
</style>
